package cn.htcode.controller;

import cn.htcode.domain.Product;
import cn.htcode.domain.User;
import cn.htcode.service.IProductService;
import cn.htcode.service.imp.ProductImpl;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author: Mr_he
 * @Version:1.0
 */
@WebServlet("/product")
public class ProductController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //        发起请求的时候，需要能够获取到返回
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        //搜索栏中传进来的名字
        String name = req.getParameter("name");
        IProductService service = new ProductImpl();
        List<Product> list = service.list(currentPage, pageSize, name);
        PageInfo<Product> info = new PageInfo<>(list);
//        String s = JSON.toJSONString(info);
        System.out.println(service.list(currentPage, pageSize, name));
        String json = JSON.toJSONString(service.list(currentPage, pageSize, name));
        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(json);
    }

    //新增操作
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String price = req.getParameter("price");
        String remark = req.getParameter("remark");
        User user = (User) req.getSession().getAttribute("user");
        IProductService service = new ProductImpl();
        Product product = new Product();
        product.setName(name);
        product.setPrice(Double.parseDouble(price));
        product.setRemark(remark);
        product.setCreateId(user.getId());
        product.setCreateTime(new Date());
        int insert = service.insert(product);
        resp.getWriter().write(insert > 0 ? "ok" : "on");
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        final String id = req.getParameter("id");
        final String name = req.getParameter("name");
        final String price = req.getParameter("price");
        final String remark = req.getParameter("remark");
        final User user = (User) req.getSession().getAttribute("user");
        IProductService service = new ProductImpl();
        final Product product = new Product();
        product.setName(name);
        product.setRemark(remark);
        product.setPrice(Double.valueOf(price));
        product.setId(Long.valueOf(id));
        product.setUpdateId(user.getId());
        product.setUpdateTime(new Date());
        int insert = service.update(product);
        resp.getWriter().write(insert > 0 ? "ok" : "no");
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("ids");
        String[] split = id.split(",");
        IProductService service = new ProductImpl();
        int count = 0;
        for (String s : split) {
            count++;
            if (s != null && !"".equals(s.trim())) {//trim不为空的时候，循环删除
                int delete = service.delete(Long.valueOf(s));
            }
        }

        resp.getWriter().write(count > 0 ? "ok" : "no");
    }
}
